package com.tyqx.hashchainbiz.service.impl;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbiz.service.TrackDonateService;
import org.springframework.stereotype.Service;

@Service("trackDonateService")
public class TrackDonateServiceImpl implements TrackDonateService {

   /* @Resource
    private ProductInMapper productInMapper;

    @Resource
    private DonateCashMapper donateCashMapper;

    @Resource
    private DonateProductMapper donateProductMapper;

    @Resource
    private NeedProductMapper needProductMapper;


    @Resource
    private ProducePlanMapper producePlanMapper;

    @Resource
    private ProduceProductInMapper produceProductInMapper;

    @Resource
    private ProduceProductOutMapper produceProductOutMapper;

    @Resource
    private ProductOutMapper productOutMapper;

    @Resource
    private ShippingInfoMapper shippingInfoMapper;

    @Resource
    private TrackUserMapper trackUserMapper;

    @Resource
    private UseProductMapper useProductMapper;

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private BuyProductMapper buyProductMapper;

    @Resource
    private HashIndexMapper hashIndexMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Resource
    private NoHashValueJsonMapper noHashValueJsonMapper;

    @Async
    public Integer donateProduct(DonateProduct donateProduct) throws Exception {
        String[] donateIds=donateProduct.getDonateId().split(";");
        Integer count=0;
        if(donateIds.length>1){
            for (int i=0;i< donateIds.length;i++){
                Date currentDate = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date createTime=sdf.parse(sdf.format(currentDate));
                DonateProduct donateProduct1=new DonateProduct();
                donateProduct1.setCreateTime(createTime);
                donateProduct1.setUpdateTime(createTime);
                donateProduct1.setAttachment(donateProduct.getAttachment());
                donateProduct1.setDonatee(donateProduct.getDonatee());
                donateProduct1.setDonateName(donateProduct.getDonateName());
                donateProduct1.setDonateTime(donateProduct.getDonateTime());
                donateProduct1.setDonateType(donateProduct.getDonateType());
                donateProduct1.setDonateUnit(donateProduct.getDonateUnit());
                donateProduct1.setEffectTime(donateProduct.getEffectTime());
                donateProduct1.setFileNo(donateProduct.getFileNo());
                donateProduct1.setPackageNum(donateProduct.getPackageNum());
                donateProduct1.setPoint(donateProduct.getPoint());
                donateProduct1.setProduceTime(donateProduct.getProduceTime());
                donateProduct1.setProductType(donateProduct.getProductType());
                donateProduct1.setShipId(donateProduct.getShipId());
                donateProduct1.setShipper(donateProduct.getShipper());
                donateProduct1.setTemp(donateProduct.getTemp());
                donateProduct1.setUserName(donateProduct.getUserName());
                donateProduct1.setDonateId(donateIds[i]);
                String donateProductStr= donateProduct1.toString();
                HashIndex hashIndex=HashValueUtil.getHashValue(donateProductStr);
                donateProduct1.setHashValue(hashIndex.getHashValue());

                String attachmentStr=donateProduct.getAttachment();
                if (!"".equals(attachmentStr) && attachmentStr != null) {
                    String collectionName = "donateProduct";
                    String imageHashValue = ImagePathHashValueUtil.insertAttachment(attachmentStr, mongoTemplate, collectionName);
                    donateProduct.setAttachment(imageHashValue);
                }

                Integer flag=hashIndexMapper.insert(hashIndex);
                count=donateProductMapper.addProduct(donateProduct1);
            }
        }else{
            Date currentDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date createTime=sdf.parse(sdf.format(currentDate));
            donateProduct.setDonateId(donateIds[0]);
            donateProduct.setCreateTime(createTime);
            donateProduct.setUpdateTime(createTime);
            String donateProductStr= donateProduct.toString();


            String attachmentStr=donateProduct.getAttachment();
            if (!"".equals(attachmentStr) && attachmentStr != null) {
                String collectionName = "donateProduct";
                String imageHashValue = ImagePathHashValueUtil.insertAttachment(attachmentStr, mongoTemplate, collectionName);
                donateProduct.setAttachment(imageHashValue);
            }
            HashIndex hashIndex=HashValueUtil.getHashValue(donateProductStr);
            donateProduct.setHashValue(hashIndex.getHashValue());
            Integer flag=hashIndexMapper.insert(hashIndex);
            count=donateProductMapper.addProduct(donateProduct);
        }
        return count;
    }

    @Async
    public Future<String> donateCash(DonateCash donateCash) throws Exception {
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        donateCash.setCreateTime(createTime);
        donateCash.setUpdateTime(createTime);
        String donateCashStr= donateCash.toString();
        //HashIndex hashIndex=HashValueUtil.getHashValue(donateCashStr);
        HashIndex hashIndex=new HashIndex();
        for (int i=0;i<4;i++){
            int sleepNum=1000;
            hashIndex=HashValueUtil.getHashValue(donateCashStr);
            String hashValue=hashIndex.getHashValue();
            String txId=hashIndex.getTxId();
            Long blockNum=hashIndex.getBlockNum();
            if(EmptyUtils.isNotEmpty(hashValue)&&EmptyUtils.isNotEmpty(txId)&&EmptyUtils.isNotEmpty(blockNum)){
                for (int j=0;j<4;j++){
                    Integer flag=hashIndexMapper.insert(hashIndex);
                    if(flag>0){
                        break;
                    }else{
                        if(j==4){
                            //tianjia database
                            String hashIndexStr= hashIndex.toString();

                        }else{
                            sleepNum=sleepNum*j*5;
                            Thread.sleep(sleepNum);
                        }
                    }
                }
                donateCash.setHashValue(hashIndex.getHashValue());
                for (int j=0;j<4;j++){
                    Integer flag=donateCashMapper.addCash(donateCash);
                    if(flag>0){
                        break;
                    }else{
                        if(j==4){
                            //tianjia database
                            String donateCashStr1= donateCash.toString();
                        }else{
                            sleepNum=sleepNum*j*5;
                            Thread.sleep(sleepNum);
                        }
                    }
                }
                break;
            }else{
                if(i==3){

                    //tianjia database
                    NoHashValueJson noHashValueJson=new NoHashValueJson();
                    noHashValueJson.setRecordJson(donateCashStr);
                    noHashValueJson.setHashTableName("donate_cash");
                    noHashValueJsonMapper.insert(noHashValueJson);
                }else{
                    sleepNum=sleepNum*i*5;
                    Thread.sleep(sleepNum);
                }
            }
        }
        return new AsyncResult<String>("添加成功");
    }

    @Async
    public Integer productIn(ProductIn productIn)throws Exception {
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        productIn.setCreateTime(createTime);
        productIn.setUpdateTime(createTime);

//        UserContext userContext= Util.readUserContext(Config.ORG1, "user1");
//
//        HashIndex hashIndex=ChainCode.insertHash(jsonObject,userContext);
        String productInStr= productIn.toString();


        String attachmentStr=productIn.getAttachment();
        if (!"".equals(attachmentStr) && attachmentStr != null) {
            String collectionName = "productIn";
            String imageHashValue = ImagePathHashValueUtil.insertAttachment(attachmentStr, mongoTemplate, collectionName);
            productIn.setAttachment(imageHashValue);
        }
        HashIndex hashIndex=HashValueUtil.getHashValue(productInStr);
        Integer flag=hashIndexMapper.insert(hashIndex);
        productIn.setHashValue(hashIndex.getHashValue());
        return productInMapper.addInProduct(productIn);
    }

    @Async
    public Integer productOut(ProductOut productOut) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        productOut.setCreateTime(createTime);
        productOut.setUpdateTime(createTime);
        String productOutStr= productOut.toString();


        String attachmentStr=productOut.getAttachment();
        if (!"".equals(attachmentStr) && attachmentStr != null) {
            String collectionName = "productOut";
            String imageHashValue = ImagePathHashValueUtil.insertAttachment(attachmentStr, mongoTemplate, collectionName);
            productOut.setAttachment(imageHashValue);
        }
        HashIndex hashIndex=HashValueUtil.getHashValue(productOutStr);
        Integer flag=hashIndexMapper.insert(hashIndex);

        productOut.setHashValue(hashIndex.getHashValue());
        return productOutMapper.addOutProduct(productOut);
    }

    @Async
    public Integer useProduct(UseProduct useProduct) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        useProduct.setCreateTime(createTime);
        useProduct.setUpdateTime(createTime);
        String useProductStr= useProduct.toString();


        String attachmentStr=useProduct.getAttachment();
        if (!"".equals(attachmentStr) && attachmentStr != null) {
            String collectionName = "useProduct";
            String imageHashValue = ImagePathHashValueUtil.insertAttachment(attachmentStr, mongoTemplate, collectionName);
            useProduct.setAttachment(imageHashValue);
        }
        HashIndex hashIndex=HashValueUtil.getHashValue(useProductStr);
        Integer flag=hashIndexMapper.insert(hashIndex);

        useProduct.setHashValue(hashIndex.getHashValue());
        return useProductMapper.addUseProduct(useProduct);
    }

    @Async
    public Integer needProduct(NeedProduct needProduct) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        needProduct.setCreateTime(createTime);
        needProduct.setUpdateTime(createTime);
        String needProductStr= needProduct.toString();
        HashIndex hashIndex=HashValueUtil.getHashValue(needProductStr);
        Integer flag=hashIndexMapper.insert(hashIndex);
        needProduct.setHashValue(hashIndex.getHashValue());

        return needProductMapper.addNeedProduct(needProduct);
    }

    @Async
    public Integer buyProduct(BuyProduct buyProduct) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        buyProduct.setCreateTime(createTime);
        buyProduct.setUpdateTime(createTime);
        String buyProductStr= buyProduct.toString();
        String attachmentStr=buyProduct.getAttachment();
        if (!"".equals(attachmentStr) && attachmentStr != null) {
            String collectionName = "buyProduct";
            String imageHashValue = ImagePathHashValueUtil.insertAttachment(attachmentStr, mongoTemplate, collectionName);
            buyProduct.setAttachment(imageHashValue);
        }
        HashIndex hashIndex=HashValueUtil.getHashValue(buyProductStr);
        Integer flag=hashIndexMapper.insert(hashIndex);

        buyProduct.setHashValue(hashIndex.getHashValue());
        return buyProductMapper.addBuyProduct(buyProduct);
    }

    @Async
    public Integer producePlan(ProducePlan producePlan) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        producePlan.setCreateTime(createTime);
        producePlan.setUpdateTime(createTime);
        String producePlanStr= producePlan.toString();
        HashIndex hashIndex=HashValueUtil.getHashValue(producePlanStr);
        Integer flag=hashIndexMapper.insert(hashIndex);

        producePlan.setHashValue(hashIndex.getHashValue());

        return producePlanMapper.addProducePlan(producePlan);
    }

    @Async
    public Integer produceProductIn(ProduceProductIn produceProductIn) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        produceProductIn.setCreateTime(createTime);
        produceProductIn.setUpdateTime(createTime);
        String produceProductInStr= produceProductIn.toString();
        HashIndex hashIndex=HashValueUtil.getHashValue(produceProductInStr);
        Integer flag=hashIndexMapper.insert(hashIndex);

        produceProductIn.setHashValue(hashIndex.getHashValue());

        return produceProductInMapper.addProduceProductIn(produceProductIn);
    }

    @Async
    public Integer produceProductOut(ProduceProductOut produceProductOut) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        produceProductOut.setCreateTime(createTime);
        produceProductOut.setUpdateTime(createTime);
        String produceProductOutStr= produceProductOut.toString();
        HashIndex hashIndex=HashValueUtil.getHashValue(produceProductOutStr);
        Integer flag=hashIndexMapper.insert(hashIndex);

        produceProductOut.setHashValue(hashIndex.getHashValue());

        return produceProductOutMapper.addProduceProductOut(produceProductOut);
    }

    @Async
    public Integer shippingProduct(ShippingInfo shippingInfo) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        shippingInfo.setCreateTime(createTime);
        shippingInfo.setUpdateTime(createTime);
        String shippingInfoStr= shippingInfo.toString();
        HashIndex hashIndex=HashValueUtil.getHashValue(shippingInfoStr);
        Integer flag=hashIndexMapper.insert(hashIndex);

        shippingInfo.setHashValue(hashIndex.getHashValue());

        return shippingInfoMapper.addShippingInfo(shippingInfo);
    }

    @Async
    public Integer addJZTLogistics(JZTLogistics jztLogistics) throws Exception {
        return null;
    }*/
}
